local que_jiezha = fk.CreateSkill {
    name = "que_jiezha",
    anim_type = "control",
}
Fk:loadTranslationTable {
    ["que_jiezha"] = "竭绽",
    [":que_jiezha"] = "出牌阶段开始时，你可以失去一点体力，向〖血噬〗中添加一张任意非装备牌名并发动对应效果；你使用〖血噬〗已记录牌名的牌次数+1。",

    ["@$que_xueshi"] = "血誓",
}
local U = require "packages/utility/utility"
que_jiezha:addEffect(fk.EventPhaseStart, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(que_jiezha.name) and player.phase == Player.Play
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {
            skill_name = que_jiezha.name,
            prompt = "竭绽：是否失去一点体力，向〖血噬〗中增加一种牌名？"
        })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:loseHp(player, 1, que_jiezha.name)
        local mark = player:getTableMark("@$que_xueshi")
        local name = U.askForChooseCardNames(room, player, Fk:getAllCardNames("btd", true), 1, 1, que_jiezha.name,
            "#difa-choice")[1]
        room:addTableMark(player, "@$que_xueshi", name)
        mark = player:getTableMark("@$que_xueshi")
        local n = 0
        for _, id in ipairs(mark) do
            local card = Fk:cloneCard(id)
            if card.trueName == name then
                n = n + 1
            end
        end
        if n == 1 then
            player:drawCards(1, que_jiezha.name)
        end
        if n == 3 then
            for i = 1, n, 1 do
                room:removeTableMark(player, "@$que_xueshi", name)
            end
            player:drawCards(n, que_jiezha.name)
        end
    end,
})
que_jiezha:addEffect("targetmod", {
    residue_func = function(self, player, skill, scope)
        local mark = player:getTableMark("@$que_xueshi")
        local n = 0
        local x = 0
        for _, id in ipairs(mark) do
            local card = Fk:cloneCard(id)
            if card.trueName == "analeptic" then
                n = n + 1
            end
            if card.trueName == "slash" then
                x = x + 1
            end
        end
        if skill.trueName == "slash_skill" and x > 0 and scope == Player.HistoryPhase then
            return 1
        end
        if skill.trueName == "analeptic_skill" and n > 0 and scope == Player.HistoryTurn then
            return 1
        end
    end,
})

return que_jiezha
